package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.application.TclApplication;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.SoftKeyboardUtils;
import com.captainbank.tcl.tclapplication.utils.leakcanary.LeaksManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by TCL on 2019/6/17.
 * Describe:
 */
public class LeakcanaryActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_show)
    TextView tvShow;

    LeaksManager leaksManager;
    @BindView(R.id.cb_keyboard)
    CheckBox cbKeyboard;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_leakcanary;
    }

    @Override
    protected void initData() {
        TclApplication.getRefWatcher().watch(this);
    }

    @Override
    protected void initView() {
        toolbar.setTitle("Leakcanary");
    }

    @Override
    protected void setListener() {
        tvShow.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                tvShow.setText("");
                return true;
            }
        });
    }

    @OnClick({R.id.btn_unsafe, R.id.btn_safe})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_unsafe:
                leaksManager = LeaksManager.getInstance(this);
                tvShow.setText("点击了‘内存泄漏’的获取方法");
                break;
            case R.id.btn_safe:
                leaksManager = LeaksManager.getInstanceSafe(this);
                tvShow.setText("点击了‘内存安全’的获取方法");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        //软键盘导致的内存泄漏
        if(cbKeyboard.isChecked()){
            SoftKeyboardUtils.fixInputMethodManagerLeak(this);
            System.out.println("输出：防止软键盘导致的内存泄漏");
        }else{
            System.out.println("输出：可能会出现软键盘导致的内存泄漏");
        }
        super.onDestroy();
    }
}
